package com.sw.funfuck.common.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * JacksonUtil
 *
 * @author Johnathan
 */
public class JacksonUtil {

    private static final Logger logger = LoggerFactory.getLogger(JacksonUtil.class);

    /**
     * 序列化对象 为 json 字符串
     * @param object
     * @return
     */
    public static String writeObjectToJSON(Object object) {
        ObjectMapper objectMapper = createObjectMapper();

        String json = null;

        try {
            json = objectMapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            logger.error("cannot write object to json value.", e);
        }

        return json;
    }

    /**
     * 序列化对象 为 json 字符串（并格式化）
     * @param object
     * @return
     */
    public static String writeObjectToPerttyJSON(Object object) {
        ObjectMapper objectMapper = createObjectMapper();

        String json = null;

        try {
            json = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(object);
        } catch (JsonProcessingException e) {
            logger.error("cannot write object to json value.", e);
        }

        return json;
    }


    /**
     * 反序列化对象
     * @param objectJson
     * @param valueType
     * @param <T>
     * @return
     */
    public static <T> T readObjectFromJSON(String objectJson, Class<T> valueType) {
        ObjectMapper objectMapper = createObjectMapper();

        try {
            return objectMapper.readValue(objectJson, valueType);
        } catch (IOException e) {
            logger.error("cannot read json value:{} to {}.", objectJson, valueType, e);
        }

        return null;
    }

    /**
     * 反序列化对象
     * @param objectJson
     * @param valueTypeRef
     * @param <T>
     * @return
     */
    public static <T> T readObjectFromJSON(String objectJson, TypeReference<?> valueTypeRef) {
        ObjectMapper objectMapper = createObjectMapper();

        try {
            return objectMapper.readValue(objectJson, valueTypeRef);
        } catch (IOException e) {
            logger.error("cannot read json value:{} to {}.", objectJson, valueTypeRef, e);
        }

        return null;
    }

    private static ObjectMapper createObjectMapper(){
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        return objectMapper;
    }

    /**
     * 对象转换为map
     * @param object
     * @return
     */
    public static Map<String, Object> object2Map(Object object){
        JSONObject jsonObject = (JSONObject) JSON.toJSON(object);
        Set<Map.Entry<String,Object>> entrySet = jsonObject.entrySet();
        Map<String, Object> map=new HashMap<String,Object>();
        for (Map.Entry<String, Object> entry : entrySet) {
            map.put(entry.getKey(), entry.getValue());
        }
        return map;
    }
}
